﻿using System.Collections;
using System.Reflection;
using System.Web.Mvc;
using Kratos.Framework.Extensions.RouteExtensions;
using Kratos.Framework.Infrastructures;
using Kratos.Framework.Modules.Core.Services;
using Kratos.Framework.Modules.Core.Services.Impl;
using Microsoft.Practices.Unity;

namespace Kratos.Framework.Modules
{
    public class KratosModule : IKratosModule
    {
        private readonly IUnityContainer _container;

        public KratosModule(IUnityContainer container)
        {
            _container = container;
        }

        public void Initialize()
        {
            SetDependencyResolver();
        }

        public void RegisterRoutes(System.Web.Routing.RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //优先级最低
            routes.MapLowerCaseRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
        }

        public void RegisterModelBinders(ModelBinderDictionary modelBinders)
        {

        }

        private void SetDependencyResolver()
        {
            DependencyResolver.SetResolver(new KratosDependencyResolver(_container));
        }

        public void RegisterBizServices()
        {
            _container.RegisterType<IActionPermissionService, ActionPermissionService>();
            _container.RegisterType<IUserInfoServices, UserInfoServices>();
        }

        public void RegisterAction()
        {
            var assembly = Assembly.GetExecutingAssembly();
            SecurityHelper.SetActionByAssembly(assembly);
        }

        public void Unload()
        {

        }
    }
}
